package com.example.sxt_practise.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 利用发射读取注解信息
 * @author gaofeng
 * @date 2022年06月05日 9:47
 */
public class AnnotationDemo {
    public static void main(String[] args) {
        try {
            // 获取类的所有注解
            System.out.println();
            Class<?> clazz = Class.forName("annotation.MyStudent");
            Annotation[] annotations = clazz.getAnnotations();
            System.out.println(annotations.length);
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }

            // 获取类的指定的注解
            Annotation annotation = clazz.getAnnotation(MyTable.class);
            System.out.println("取类的指定的注解 -> " + annotation);

            // 获取类的属性的注解
            Field stuName = clazz.getDeclaredField("stuName");
            MyField annotation1 = stuName.getAnnotation(MyField.class);
            System.out.println(annotation1);

        } catch (ClassNotFoundException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}
